#include "LoopsController.h"

LoopsController::LoopsController ():
	m_LoopCounter(0),
	m_TimeOnLoopBegin(0),
	m_TimeCurrent(0),
	m_WasUpdate(0)
{
}

LoopsController::~LoopsController()
{

}

void LoopsController::Run(System *system, Game *game, Renderer *renderer)
{
	m_TimeCurrent = timeGetTime();
	while( !game->IsQuit() && !system->IsQuit() ) {
		m_TimeOnLoopBegin = timeGetTime();
		m_LoopCounter = 0;
		while( (m_TimeOnLoopBegin - m_TimeCurrent) > m_TICK_TIME && m_LoopCounter < m_MAX_LOOPS ) {

		    system->HandleEvents();
			game->Update();

			m_WasUpdate = 1;
			m_TimeCurrent += m_TICK_TIME;
			m_LoopCounter++;
		}
		if( m_WasUpdate == 1 ){
		    system->UpdateScreen();
			game->Render (renderer);
		}
		else
			Sleep(1);
		m_WasUpdate = 0;
	}
}
